﻿@{
    Layout = null;
    int currentPage = ViewBag.CurrentPage ?? 1;
    int totalPages = ViewBag.TotalPages ?? 1;
}

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>书香阁 - 图书商城</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>   
    
    
    <style>
        /* 基础样式 */
        :root {
            --primary-color: #6c5ce7;
            --primary-dark: #5649c0;
            --secondary-color: #a8a4e6;
            --text-color: #2d3436;
            --light-bg: #f8f9fa;
            --card-shadow: 0 8px 24px rgba(0,0,0,0.08);
            --radius: 12px;
            --transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Noto Serif SC', serif;
            color: var(--text-color);
            background: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%);
            line-height: 1.6;
            min-height: 100vh;
        }

        /* 布局容器 */
        .container {
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }

        .category-link.active {
            background: var(--primary-color) !important;
            color: white !important;
        }
        /* 侧边栏样式 */
        .sidebar {
            width: 280px;
            background: white;
            padding: 2rem;
            box-shadow: 4px 0 12px rgba(0,0,0,0.06);
            position: fixed;
            height: 100vh;
            overflow-y: auto;
            z-index: 100;
            transition: var(--transition);
            /* 可选：平滑滚动 */
            scroll-behavior: smooth;
            /* 移动端惯性滚动 */
            -webkit-overflow-scrolling: touch;
        }

            /* =============== 滚动条样式 =============== */

            /* 整体轨道宽度 */
            .sidebar::-webkit-scrollbar {
                width: 8px; /* 滚动条宽度 */
            }

            /* 滚动条滑块（拖拽条） */
            .sidebar::-webkit-scrollbar-thumb {
                background-color: #ccc; /* 默认颜色 */
                border-radius: 4px; /* 圆角 */
                background-clip: padding-box;
            }

                /* 滚动条滑块悬停时的样式 */
                .sidebar::-webkit-scrollbar-thumb:hover {
                    background-color: #aaa; /* 悬停更深的颜色 */
                }

            /* 滚动条轨道 */
            .sidebar::-webkit-scrollbar-track {
                background: #f4f4f4;
                border-radius: 4px;
            }

        .sidebar-section {
            margin-bottom: 2rem;
        }

        .sidebar-title {
            font-size: 1.1rem;
            font-weight: 700;
            color: var(--primary-color);
            margin-bottom: 1rem;
            padding-bottom: 0.5rem;
            border-bottom: 2px solid var(--primary-color);
        }

        .category-list {
            list-style: none;
            padding: 0;
        }

        .category-item {
            margin: 0.5rem 0;
        }

        .category-link {
            display: block;
            padding: 0.8rem 1rem;
            border-radius: 6px;
            color: var(--text-color);
            text-decoration: none;
            transition: var(--transition);
            position: relative;
        }

            .category-link:hover {
                background: var(--light-bg);
                color: var(--primary-color);
                transform: translateX(8px);
            }

        /* 主内容区 */
        .main-content {
            flex: 1;
            margin-left: 280px;
            padding-left: 1rem;
            margin-top: 10px;
            transition: var(--transition);
        }

        /* 顶部导航 */
        .main-header {
            background: white;
            padding: 1rem 2rem;
            box-shadow: 0 4px 12px rgba(0,0,0,0.06);
            position: sticky;
            top: 0;
            z-index: 99;
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 0.5rem;
        }

        .search-container {
            flex: 1;
            max-width: 600px;
            margin: 0 2rem;
            position: relative;
        }

        .search-form {
            display: flex;
            align-items: center;
        }

        .search-select {
            width: 100%;
            padding: 0.8rem 1.2rem;
            border: 2px solid #e0e0e0;
            border-radius: 30px 0 0 30px;
            font-size: 1rem;
            transition: var(--transition);
            outline: none;
            font-family: inherit;
        }

            .search-select:focus {
                border-color: var(--primary-color);
            }

        .search-btn {
            padding: 0.8rem 1.5rem;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: 0 30px 30px 0;
            cursor: pointer;
            transition: var(--transition);
            font-family: inherit;
        }

            .search-btn:hover {
                background: var(--primary-dark);
            }

        /* 用户菜单 */
        .user-menu {
            position: relative;
        }

        .account-btn {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            background: none;
            border: none;
            padding: 0.5rem 1rem;
            border-radius: 30px;
            cursor: pointer;
            transition: var(--transition);
            font-family: inherit;
            color: var(--text-color);
        }

            .account-btn:hover {
                background: var(--light-bg);
            }

        .dropdown-menu {
            position: absolute;
            right: 0;
            top: 120%;
            background: white;
            box-shadow: var(--card-shadow);
            border-radius: var(--radius);
            min-width: 180px;
            opacity: 0;
            visibility: hidden;
            transition: var(--transition);
            z-index: 101;
            padding: 0.5rem 0;
        }

        .dropdown-item {
            display: block;
            padding: 0.8rem 1.5rem;
            color: var(--text-color);
            text-decoration: none;
            transition: var(--transition);
        }

            .dropdown-item:hover {
                background: var(--light-bg);
                color: var(--primary-color);
            }

        .user-menu:hover .dropdown-menu {
            opacity: 1;
            visibility: visible;
            top: 100%;
        }

        /* 内容区域 */
        .content-wrapper {
            display: flex;
            width: 100%;
        }

        /* 图书网格 */
        .book-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
            gap: 1rem; /* 水平间距 */
            width: 100%;
            margin: 0;
            padding: 0;
        }

        /* 图书卡片 */
        .book-card {
            background: white;
            border-radius: var(--radius);
            box-shadow: var(--card-shadow);
            overflow: hidden;
            transition: var(--transition);
            height: 100%;
            display: flex;
            flex-direction: column;
            position: relative;
        }

            .book-card:hover {
                transform: translateY(-5px);
                box-shadow: 0 12px 32px rgba(0,0,0,0.12);
            }

        /* 图片容器 */
        .image-container {
            height: 230px;
            overflow: hidden;
            position: relative;
            border-bottom: 3px solid var(--primary-color);
            cursor: pointer;
        }

        .book-cover {
            width: 100%;
            height: 100%;
            object-fit: contain;
            transition: var(--transition);
        }

        .book-card:hover .book-cover {
            transform: scale(1.05);
        }

        /* 卡片内容 */
        .book-info {
            padding: 0.8rem;
            flex: 1;
            display: flex;
            flex-direction: column;
        }

        .book-title {
            font-size: 1.2rem;
            font-weight: 700;
            margin-bottom: 0.5rem;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .book-meta {
            font-size: 0.9rem;
            color: #636e72;
            margin-bottom: 0.2rem;
        }

            .book-meta i {
                margin-right: 0.3rem;
                color: var(--primary-color);
                width: 1rem;
                text-align: center;
            }

        .book-price {
            color: var(--primary-color);
            font-size: 1.4rem;
            font-weight: 700;
            margin: 0.2rem 0;
        }

        .action-btn {
            display: inline-block;
            padding: 0.6rem 1.1rem;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: var(--radius);
            text-decoration: none;
            text-align: center;
            cursor: pointer;
            transition: var(--transition);
            margin-top: auto;
            font-family: inherit;
        }

            .action-btn:hover {
                background: var(--primary-dark);
                transform: translateY(-2px);
            }



        /* 菜单切换按钮 */
        .menu-toggle {
            display: none;
            position: fixed;
            top: 1rem;
            left: 1rem;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            font-size: 1.2rem;
            cursor: pointer;
            z-index: 1001;
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
        }

        .logo {
            font-size: 1.8rem;
            font-weight: 700;
            color: var(--primary-color);
            text-decoration: none;
        }


        /* 覆盖层 */
        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            z-index: 999;
            opacity: 0;
            visibility: hidden;
            transition: var(--transition);
        }

            .overlay.active {
                opacity: 1;
                visibility: visible;
            }

        .search-input {
            width: 100%;
            padding: 0.8rem 1.2rem;
            border: 2px solid #e0e0e0;
            border-radius: 30px 0 0 30px;
            font-size: 1rem;
            transition: var(--transition);
            outline: none;
            font-family: inherit;
        }

            .search-input:focus {
                border-color: var(--primary-color);
            }

        /* 添加搜索建议框样式 */
        .search-suggestions {
            position: absolute;
            top: 100%;
            left: 0;
            right: 0;
            background: white;
            border-radius: 0 0 var(--radius) var(--radius);
            box-shadow: var(--card-shadow);
            z-index: 100;
            max-height: 300px;
            overflow-y: auto;
            display: none;
        }

        .suggestion-item {
            padding: 0.8rem 1.2rem;
            cursor: pointer;
            transition: var(--transition);
        }

            .suggestion-item:hover {
                background: var(--light-bg);
                color: var(--primary-color);
            }

        .menu-toggle {
            display: none;
            position: fixed;
            top: 1rem;
            left: 1rem;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            font-size: 1.2rem;
            cursor: pointer;
            z-index: 1001;
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
            transition: var(--transition);
        }

            .menu-toggle:hover {
                background: var(--primary-dark);
                transform: scale(1.1);
            }

        /* 分页容器 */
        .pagination-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin: 20px 0;
            padding: 10px 0;
        }

        /* 分页信息 */
        .pagination-info {
            color: #666;
            font-size: 14px;
        }

        /* 分页链接 */
        .pagination {
            display: flex;
            gap: 5px;
        }

        .page-link {
            display: inline-block;
            padding: 5px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            color: #333;
            text-decoration: none;
            transition: all 0.3s;
        }

            .page-link:hover:not(.disabled):not(.active) {
                background-color: #f5f5f5;
            }

            .page-link.active {
                background-color: #007bff;
                color: white;
                border-color: #007bff;
            }

            .page-link.disabled {
                color: #ccc;
                cursor: not-allowed;
            }

            .page-link.first, .page-link.last, .page-link.prev, .page-link.next {
                padding: 5px 10px;
            }
    </style>
</head>
<body>
    <div class="container">
        <!-- 菜单切换按钮 (移动端) -->
        <button class="menu-toggle" id="menuToggle" aria-label="切换菜单">
            <i class="fas fa-bars"></i>
        </button>

        <!-- 覆盖层 (移动端) -->
        <div class="overlay" id="overlay"></div>

        <!-- 内容区域 -->
        <div class="content-wrapper">
            <!-- 左侧侧边栏 -->
            <aside class="sidebar" id="sidebar">
                <div class="sidebar-section">
                    <h3 class="sidebar-title">图书分类</h3>
                    <ul class="category-list">
                        <li class="category-item">
                            <a href="@Url.Action("Index", new { page = 1, category = "all" })"
                               class="category-link @(ViewBag.CurrentCategory == "all" ? "active" : "")">全部图书</a>

                        </li>
                        <li class="category-item">
                            <a href="@Url.Action("Index", new { page = 1, category = "教材" })"
                               class="category-link @(ViewBag.CurrentCategory == "教材" ? "active" : "")">教材</a>
                        </li>
                        <li class="category-item">
                            <a href="@Url.Action("Index", new { page = 1, category = "文学" })"
                               class="category-link @(ViewBag.CurrentCategory == "文学" ? "active" : "")">文学</a>
                        </li>
                        <li class="category-item">
                            <a href="@Url.Action("Index", new { page = 1, category = "科幻" })"
                               class="category-link @(ViewBag.CurrentCategory == "科幻" ? "active" : "")">科幻</a>
                        </li>
                        <li class="category-item">
                            <a href="@Url.Action("Index", new { page = 1, category = "小说" })"
                               class="category-link @(ViewBag.CurrentCategory == "小说" ? "active" : "")">小说</a>
                        </li>
                        <li class="category-item">
                            <a href="@Url.Action("Index", new { page = 1, category = "医学" })"
                               class="category-link @(ViewBag.CurrentCategory == "医学" ? "active" : "")">医学</a>
                        </li>
                        <li class="category-item">
                            <a href="@Url.Action("Index", new { page = 1, category = "物理" })"
                               class="category-link @(ViewBag.CurrentCategory == "物理" ? "active" : "")">物理</a>
                        </li>
                        <li class="category-item">
                            <a href="@Url.Action("Index", new { page = 1, category = "生物" })"
                               class="category-link @(ViewBag.CurrentCategory == "生物" ? "active" : "")">生物</a>
                        </li>
                        <li class="category-item">
                            <a href="@Url.Action("Index", new { page = 1, category = "古装武侠" })"
                               class="category-link @(ViewBag.CurrentCategory == "古装武侠" ? "active" : "")">古装武侠</a>
                        </li>
                        <li class="category-item">
                            <a href="@Url.Action("Index", new { page = 1, category = "历史" })"
                               class="category-link @(ViewBag.CurrentCategory == "历史" ? "active" : "")">历史</a>
                        </li>
                        <li class="category-item">
                            <a href="@Url.Action("Index", new { page = 1, category = "科学技术" })"
                               class="category-link @(ViewBag.CurrentCategory == "科学技术" ? "active" : "")">科学技术</a>
                        </li>
                        <li class="category-item">
                            <a href="@Url.Action("Index", new { page = 1, category = "数理科学和化学" })"
                               class="category-link @(ViewBag.CurrentCategory == "数理科学和化学" ? "active" : "")">数理科学和化学</a>
                         </li>
                    </ul>
                </div>

                <div class="sidebar-section">
                    <h3 class="sidebar-title">热门筛选</h3>
                    <ul class="category-list">
                        <li class="category-item">
                            <a href="#" class="category-link">新书上市</a>
                        </li>
                        <li class="category-item">
                            <a href="#" class="category-link">畅销榜单</a>
                        </li>
                        <li class="category-item">
                            <a href="#" class="category-link">特价优惠</a>
                        </li>
                    </ul>
                </div>
            </aside>

            <!-- 主内容区 -->
            <div class="main-content">
                <!-- 顶部导航 -->
                <header class="main-header">
                    <a href="#" class="logo">广软智慧书城</a>
                    <div class="search-container">
                        <form action="~/BookDetailsInfo/SearchBooks" method="get" class="search-form" id="searchForm">
                            <input type="text" name="keyword" class="search-input" placeholder="输入书名、作者或出版社..." autocomplete="off">
                            <button type="submit" class="search-btn">
                                <i class="fas fa-search"></i>
                            </button>
                        </form>
                    </div>

                    <div class="user-menu">
                        @if (Session["ShopName"] != null)
                        {
                            <button class="account-btn">
                                <i class="fas fa-user-circle"></i>
                                <span>@Session["ShopName"]</span>
                            </button>
                            <div class="dropdown-menu">
                                @Html.ActionLink("个人中心", "Actions", new { id = Session["Id"] }, new { @class = "dropdown-item" })
                                @Html.ActionLink("退出登录", "Index", "ShopInfo", null, new { @class = "dropdown-item" })
                            </div>
                        }
                        else
                        {
                            <a href="/ShopInfo/Login" class="account-btn">
                                <i class="fas fa-sign-in-alt"></i>
                                <span>登录/注册</span>
                            </a>
                        }
                    </div>
                </header>

                <!-- 图书列表 -->
                <div class="book-grid">
                    @foreach (var item in Model)
                    {
                        <div class="book-card">
                            <a href="@Url.Action("GetBookDetails", new { id = item.Id })" class="image-container">
                                <img src="@item.Bookcover" class="book-cover" alt="@item.BookName 封面">
                            </a>
                            <div class="book-info">
                                <h3 class="book-title">@item.BookName</h3>
                                <div class="book-meta">
                                    <div><i class="fas fa-user-edit"></i> @item.BookAuthor</div>
                                    <div><i class="fas fa-building"></i> @item.Bookpress</div>
                                    <div><i class="fas fa-tag"></i> @item.Booksort</div>
                                </div>
                                <div class="book-price">¥@item.BookPrice</div>
                                @Html.ActionLink("立即购买", "GetBookDetails", new { id = item.Id }, new { @class = "action-btn" })
                            </div>
                        </div>
                    }
                </div>
                <!-- 分页控件 - 底部 -->
                <div class="pagination" style="justify-content:center">
                    @if (ViewBag.CurrentPage > 1)
                    {
                        <a href="@Url.Action("Index", new { page = 1, category = ViewBag.CurrentCategory })"
                           class="page-link">首页</a>
                        <a href="@Url.Action("Index", new { page = ViewBag.CurrentPage - 1, category = ViewBag.CurrentCategory })"
                           class="page-link">上一页</a>
                    }

                    @for (int i = 1; i <= ViewBag.TotalPages; i++)
                    {
                        <a href="@Url.Action("Index", new { page = i, category = ViewBag.CurrentCategory })"
                           class="page-link @(i == ViewBag.CurrentPage ? "active" : "")">@i</a>
                    }

                    @if (ViewBag.CurrentPage < ViewBag.TotalPages)
                    {
                        <a href="@Url.Action("Index", new { page = ViewBag.CurrentPage + 1, category = ViewBag.CurrentCategory })"
                           class="page-link">下一页</a>
                        <a href="@Url.Action("Index", new { page = ViewBag.TotalPages, category = ViewBag.CurrentCategory })"
                           class="page-link">末页</a>
                    }
                </div>


            </div>
        </div>




    </div>







    <script>





  









        function filterBooks(category) {
          window.location.href = `/Book/Index?page=1&category=${encodeURIComponent(category)}`;
        }

        // 搜索功能
        const searchForm = document.getElementById('searchForm');
        const searchInput = document.querySelector('.search-input');
        const searchSuggestions = document.createElement('div');
        searchSuggestions.className = 'search-suggestions';
        searchForm.appendChild(searchSuggestions);

        // 输入建议
        // 修改输入事件处理
        searchInput.addEventListener('input', async function () {
            const keyword = this.value.trim();

            if (keyword.length < 2) {
                searchSuggestions.style.display = 'none';
                return;
            }

            try {
                const response = await fetch(`/BookDetailsInfo/GetSearchSuggestions?keyword=${encodeURIComponent(keyword)}`);
                if (!response.ok) throw new Error('请求失败');

                const suggestions = await response.json();

                // 显示建议项
                searchSuggestions.innerHTML = suggestions.map(item => `
            <div class="suggestion-item" data-keyword="${item.BookName}">
                <div class="suggestion-title">${item.BookName}</div>
                <div class="suggestion-meta">${item.Author} | ${item.Press}</div>
            </div>
        `).join('');

                searchSuggestions.style.display = 'block';
            } catch (error) {
                console.error('获取建议失败:', error);
                searchSuggestions.style.display = 'none';
            }
        });

        // 修改表单提交处理
        searchForm.addEventListener('submit', async function (e) {
            e.preventDefault();

            const keyword = searchInput.value.trim();
            if (!keyword) return;

            try {
                const response = await fetch(`/BookDetailsInfo/SearchBooks?keyword=${encodeURIComponent(keyword)}`, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest' // 标识AJAX请求
                    }
                });

                if (!response.ok) throw new Error('搜索失败');

                const html = await response.text();
                document.querySelector('.book-grid').innerHTML = html;
                searchSuggestions.style.display = 'none';
            } catch (error) {
                console.error('搜索错误:', error);
                // 这里可以添加错误提示
            }
        });
        // 点击建议项
        // 修改建议项的点击处理
        searchSuggestions.addEventListener('click', function (e) {
            // 检查点击的是否是建议项或其子元素
            let suggestionItem = e.target.closest('.suggestion-item');
            if (suggestionItem) {
                // 获取书名（从data-keyword属性获取）
                const keyword = suggestionItem.dataset.keyword;
                searchInput.value = keyword;

                // 触发搜索
                const bookGrid = document.querySelector('.book-grid');
                bookGrid.innerHTML = '<div style="grid-column:1/-1;text-align:center;padding:2rem;">搜索中...</div>';

                fetch(`/BookDetailsInfo/SearchBooks?keyword=${encodeURIComponent(keyword)}`, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest'
                    }
                })
                    .then(response => response.text())
                    .then(html => {
                        bookGrid.innerHTML = html;
                        searchSuggestions.style.display = 'none';
                    })
                    .catch(error => {
                        console.error('搜索错误:', error);
                        bookGrid.innerHTML = '<div style="grid-column:1/-1;text-align:center;padding:2rem;">搜索失败</div>';
                    });
            }
        });

        // 点击其他地方隐藏建议框
        document.addEventListener('click', function (e) {
            if (!searchForm.contains(e.target)) {
                searchSuggestions.style.display = 'none';
            }
        });

        // 移动端菜单切换
        const menuToggle = document.getElementById('menuToggle');
        const sidebar = document.getElementById('sidebar');
        const overlay = document.getElementById('overlay');

        menuToggle.addEventListener('click', () => {
            sidebar.classList.toggle('active');
            overlay.classList.toggle('active');
        });

        overlay.addEventListener('click', () => {
            sidebar.classList.remove('active');
            overlay.classList.remove('active');
        });

        // 用户登录状态处理
        window.onload = function () {
            var data = "@Session["ShopName"]";
            var dl = document.getElementById('dl');
            var account = document.querySelector('.account');
            var accountname = document.querySelector('.accountname');
            var box = document.querySelector('.box');

            if (account) account.style.display = "none";

            if (box) {
                box.onmouseleave = function () {
                    if (account) account.style.display = "none";
                }
            }

            if (data == "") {
                if (dl) dl.innerHTML = '登录';
            }

            if (data != '') {
                if (dl) dl.innerHTML = data;
            }

            if (dl && dl.innerHTML == '登录') {
                dl.onclick = function () {
                    window.location = "/ShopInfo/Login";
                }
            } else if (dl && dl.innerHTML == data) {
                if (accountname) {
                    accountname.onclick = function () {
                        if (account) account.style.display = "block";
                    }
                }
            }

            // 图片懒加载
            const lazyLoad = () => {
                const observer = new IntersectionObserver((entries) => {
                    entries.forEach(entry => {
                        if (entry.isIntersecting) {
                            const img = entry.target;
                            img.src = img.dataset.src;
                            observer.unobserve(img);
                        }
                    });
                }, {
                    rootMargin: '0px 0px 200px 0px'
                });

                document.querySelectorAll('.book-cover').forEach(img => {
                    img.dataset.src = img.src;
                    img.src = '';
                    observer.observe(img);
                });
            };

            lazyLoad();

            // 搜索框交互
            const searchSelect = document.querySelector('.search-select');
            if (searchSelect) {
                searchSelect.addEventListener('focus', () => {
                    searchSelect.parentElement.style.transform = 'scale(1.02)';
                });
                searchSelect.addEventListener('blur', () => {
                    searchSelect.parentElement.style.transform = 'scale(1)';
                });
            }
        };

    </script>
</body>
</html>